/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.org.gundutubruk;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.RectF;

class Gundu
{
        int mass;
        public static final int diameter=30;
        //coordinates and value of increment
        public double x, y, xinc, yinc, coll_x, coll_y, coll_xinc, coll_yinc;
        // x adalah titik pojok kiri gundu
        // y adalah titik pojok kanan gundu
        boolean collide;
        // atribut penanda player
        private int player;
        int color;
        private Bitmap bitmap;
        private Point LayerSize = new Point(0,0);
        
        //counter animasi
        int canim = 0;  
        int ctime = 0;
        private Bitmap animSurface;
        private int timeBetweenAnim = 10;//waktu untuk pindah ke frame berikutnya, semakin kecil semakin cepat berubah
    	
        //counter animasi marker(penanda gundu ini yang lagi jalan)
        boolean marked = false;
        int cmarked = 0;//counter animasi marker gundu
        int ctimemarked = 0;
        private Bitmap animMark;
        private int timeBetweenAnimMark = 3;//waktu untuk pindah ke frame berikutnya
        
        //the constructor
        public Gundu(Bitmap b, int p, int m, int x, int y, double xinc, double yinc, int c, int width, int height)
        {
        	this.LayerSize.x = width;
        	this.LayerSize.y = height;
        	this.player = p;
        	this.mass = m;
        	this.x=x;
        	this.y=y;
        	this.xinc=xinc;
        	this.yinc=yinc;
        	this.color=c;
        	this.bitmap = b;
        }	
    	public Bitmap getBitmap() {
    		return bitmap;		
    	}
    	public void setBitmap(Bitmap bitmap) {
    		this.bitmap = bitmap;
    	}
    	public int isPlayer() {
    		return player;		
    	}
    	public void setPlayer(int _p) {
    		this.player = _p;
    	}
    	public boolean isStop() {
    		if ((this.xinc==0)&&(this.yinc==0)){
    			return true;
    		}
    		else return false;
    	}
        public double getCenterX() {return x+diameter/2;}
        public double getCenterY() {return y+diameter/2;}
        public void shoot(double force_x, double force_y, double fs){
            double acc_x;
            double acc_y;
            acc_x = (force_x - fs)/mass;
            acc_y = (force_y - fs)/mass;
            xinc = xinc + acc_x;
            yinc = yinc + acc_y;
        }
        
        public void move(int fr){        	
        	double acc_x=0;
            double acc_y=0;            
                x+=xinc;
                y+=yinc;                      
                animateEffect(fr,xinc,yinc);
                if (xinc>0){
                    acc_x = -(fr*1.0/mass);
                    xinc = xinc + acc_x;
                    if (xinc<0){
                        xinc=0;
                    }
                }
                else if (xinc<0){
                    acc_x = -(fr*1.0/mass);
                    xinc = xinc - acc_x;
                    if (xinc>0){
                        xinc=0;
                    }
                }
                if (yinc>0){
                    acc_y = -(fr*1.0/mass);
                    yinc = yinc + acc_y;
                    if (yinc<0){
                        yinc=0;
                    }
                }
                else if (yinc<0){
                    acc_y = -(fr*1.0/mass);
                    yinc = yinc - acc_y;
                    if (yinc>0){
                        yinc=0;
                    }
                }
                //when the ball bumps against a boundary, it bounces off
                if(x<0){
//                    xinc=-xinc;
                    x=45;
                	xinc=0;
                	yinc=0;
                    
                }
                if(x>LayerSize.x-diameter)
                {
                	
//                    xinc=-xinc;
                    x = LayerSize.x-diameter-45;
                	xinc=0;
                	yinc=0;
                    
                }

                if(y<0){
//                    yinc=-yinc;
                    y=45;
                	xinc=0;
                	yinc=0;
                }
                if(y>LayerSize.y-diameter)
                {
//                    yinc=-yinc;
                    y = LayerSize.y-diameter-45;
                	xinc=0;
                	yinc=0;
                }               
        }	

        private void animateEffect(int fr,double xinc2, double yinc2) {        	
        	if(!isStop()) {//jika tidak sedang berhenti        		
        		switch(fr) {
        		case GunduController.TANAH_BIASA :
        			//di tanah biasa tidak ada cipratan
        			animSurface = null;
        			break;
        		case GunduController.AIR :
        			animSurface = GunduController.animwater[canim];
        			break;
        		case GunduController.BATU :
        			animSurface = GunduController.animStone[canim];
        			break;
        		case GunduController.RUMPUT:
        			animSurface = GunduController.animGrass[canim];
        			break;
        		case GunduController.LUMPUR :
        			animSurface = GunduController.animMud[canim];
        			break;
        		}        	
        		ctime++;//waktu berikutnya
        		if(ctime==timeBetweenAnim) {
        			ctime = 0;
        			canim = (canim + 1) % 5;
        		}
        	}else{//jika berhenti
        		animSurface = null;
        	}
        }
        
        private void drawMarker(Canvas canvas) {
        	animMark = GunduController.animMarker[cmarked];
        	canvas.drawBitmap(animMark,(int) (x + (bitmap.getWidth()*1.0/2) - animMark.getWidth()/2),(int) (y + (bitmap.getHeight()/2) - animMark.getHeight()/2), null);
        	ctimemarked++;//waktu berikutnya
        	if(ctimemarked==timeBetweenAnimMark) {
        		ctimemarked = 0;
        		cmarked = (cmarked+1) % 14;
        	}
        }
        
        public void hit(Gundu b)
        {
        	 if(!collide)
             {   
        		 x += xinc;
        		 y += yinc;
             }
        }
        public void draw(Canvas canvas) {        	
    		//gambar gundu
        	canvas.drawBitmap(bitmap,(int)x,(int)y, null);
    		//gambar animasi di atas gundu
        	if(animSurface!=null) {
        		canvas.drawBitmap(animSurface, (int)(x-(animSurface.getWidth() - bitmap.getWidth())/2), (int)(y - (animSurface.getHeight() - bitmap.getHeight())/2),null);
        	}
        	if(isStop() && marked) {//jika gundu berhenti dan marked
        		drawMarker(canvas);
        	}
        }
        
        public void drawScaledGundu(Canvas canvas,float size) {
        	float left = (float)(this.getCenterX() - size/2);
        	float top =  (float)(this.getCenterY() - size/2);
        	canvas.drawBitmap(bitmap, null, new RectF(left,top,left+size,top+size), null);
        }
}
